﻿using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Shared
{
    [ParseChildren(true, "Item")]
    [DefaultProperty("Item")]
    [ToolboxData("<{0}:LabeledValue runat=\"server\"></{0}:LabeledValue>")]
    public class LabeledValue : WebControl
    {
        private ITemplate _item;
        private PlaceHolder _contentContainer;
        private Literal _headerLiteral;
        private HtmlGenericControl _header;
        private Literal _showInFullHeightLiteral;

        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        [TemplateInstance(TemplateInstance.Single)]
        [Browsable(false)]
        public ITemplate Item
        {
            get { return _item; }
            set
            {
                _item = value;
                _ContentContainer.Controls.Clear();
                if (value != null)
                {
                    value.InstantiateIn(_ContentContainer);
                }
            }
        }

        private PlaceHolder _ContentContainer
        {
            get
            {
                EnsureChildControls();
                return _contentContainer;
            }
        }

        /// <summary>
        /// Text to be displayed under header section for the page
        /// </summary>
        public string HeaderText
        {
            get { return HeaderLiteral.Text; }
            set { HeaderLiteral.Text = value; }
        }

        private Literal HeaderLiteral
        {
            get
            {
                EnsureChildControls();
                return _headerLiteral;
            }
        }
        
        /// <summary>
        /// To set the preference if content need to come as full page - Default set as full page;
        /// True - Show in full page;
        /// False - Can customize the height;
        /// </summary>
        public bool ShowInFullHeight
        {
            get { return ShowInFullHeightLiteral.Text != "<section class=\"content\">"; }
            set { ShowInFullHeightLiteral.Text = value ? "<section class=\"content_full\">" : "<section class=\"content\">"; }
        }

        private Literal ShowInFullHeightLiteral
        {
            get
            {
                EnsureChildControls();
                return _showInFullHeightLiteral;
            }
        }
        
        protected override void CreateChildControls()
        {
            Controls.Clear();

            _headerLiteral = new Literal();
            _header = new HtmlGenericControl("h3") {ID = "l_" + ID};
            _header.Controls.Add(_headerLiteral);
            Controls.Add(_header);

            _showInFullHeightLiteral = new Literal ();
            ShowInFullHeight = true;

            _contentContainer = new PlaceHolder { ID = "c_" + ID };
            Controls.Add(_showInFullHeightLiteral);
            Controls.Add(_contentContainer);
            Controls.Add(new Literal { Text = "</section>" });
        }
    }
}